﻿using JTActiveSafety.Gateway.Configurations;
using JTActiveSafety.Gateway.Sessions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Text.Json;
using System.IO;

namespace JTActiveSafety.Gateway.Jobs
{
    //public class JTActiveSafetyHeartbeatJob : BackgroundService
    //{
    //    private readonly ILogger Logger;

    //    private readonly JTActiveSafetySessionManager SessionManager;

    //    //private readonly JTActiveSafetyHttpSessionManager HttpSessionManager;

    //    private readonly IOptionsMonitor<JTActiveSafetyConfiguration> Configuration;

    //    private readonly JTActiveSafetyCoordinatorHttpClient CoordinatorHttpClient;
    //    public JTActiveSafetyHeartbeatJob(
    //            JTActiveSafetyCoordinatorHttpClient jT1078CoordinatorHttpClient,
    //            //JTActiveSafetyHttpSessionManager jT1078HttpSessionManager,
    //            IOptionsMonitor<JTActiveSafetyConfiguration> jT1078ConfigurationAccessor,
    //            ILoggerFactory loggerFactory,
    //            JTActiveSafetySessionManager jT1078SessionManager
    //        )
    //    {
    //        SessionManager = jT1078SessionManager;
    //        //HttpSessionManager = jT1078HttpSessionManager;
    //        Logger = loggerFactory.CreateLogger<JTActiveSafetyHeartbeatJob>();
    //        Configuration = jT1078ConfigurationAccessor;
    //        CoordinatorHttpClient = jT1078CoordinatorHttpClient;
    //    }
    //    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    //    {
    //        try
    //        {
    //            await CoordinatorHttpClient.Reset();
    //        }
    //        catch (Exception ex)
    //        {
    //            Logger.LogError(ex, $"[Coordinator Reset]");
    //        }
    //        while (!stoppingToken.IsCancellationRequested)
    //        {
    //            try
    //            {
    //                string json = "";
    //                using (var stream = new MemoryStream())
    //                {
    //                    using (var writer = new Utf8JsonWriter(stream))
    //                    {
    //                        writer.WriteStartObject();
    //                        writer.WriteNumber(nameof(Configuration.CurrentValue.HttpPort), Configuration.CurrentValue.HttpPort);
    //                        writer.WriteNumber(nameof(Configuration.CurrentValue.TcpPort), Configuration.CurrentValue.TcpPort);
    //                        writer.WriteNumber(nameof(Configuration.CurrentValue.UdpPort), Configuration.CurrentValue.UdpPort);
    //                        writer.WriteNumber(nameof(SessionManager.TcpSessionCount), SessionManager.TcpSessionCount);
    //                        writer.WriteNumber(nameof(SessionManager.UdpSessionCount), SessionManager.UdpSessionCount);
    //                        //writer.WriteNumber(nameof(HttpSessionManager.HttpSessionCount), HttpSessionManager.HttpSessionCount);
    //                        //writer.WriteNumber(nameof(HttpSessionManager.WebSocketSessionCount), HttpSessionManager.WebSocketSessionCount);
    //                        writer.WriteEndObject();
    //                    }
    //                    json = Encoding.UTF8.GetString(stream.ToArray());
    //                }
    //                if (json != "") 
    //                {
    //                    if (Logger.IsEnabled(LogLevel.Information))
    //                    {
    //                        Logger.LogInformation(json);
    //                    }
    //                    await CoordinatorHttpClient.Heartbeat(json);
    //                }
    //            }
    //            catch (Exception ex)
    //            {
    //                Logger.LogError(ex, $"[Coordinator Heartbeat]");
    //            }
    //            finally
    //            {
    //                await Task.Delay(TimeSpan.FromSeconds(Configuration.CurrentValue.CoordinatorHeartbeatTimeSeconds), stoppingToken);
    //            }
    //        }
    //    }
    //}
}
